//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.view
{
	import __AS3__.vec.Vector;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	import reader.ApplicationFacade;
	import reader.core.IGCAble;
	import reader.core.ReaderModelLocator;
	import reader.events.SearchResultEvent;
	import reader.model.IDataProxy;
	import reader.model.vo.BlogSourceVO;
	import reader.view.components.FeedSearchResult;

	public class SearchResultMediator extends BaseApplicationMediator implements IMediator, IGCAble
	{
		////////////////////////////////////////////////////////////////////////////////
		//public properties
		public static const NAME:String = "SearchResultMediator";
		
		public function get feedSearchResult():FeedSearchResult
		{
			return this.viewComponent as FeedSearchResult;
		}
		
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		private var dataProxy:IDataProxy;
		private var readerModelLocator:ReaderModelLocator = ReaderModelLocator.instance;
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function SearchResultMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			startUp();
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
		
		public function startUp():void
		{
			dataProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.dataProxyName) as IDataProxy;
			
			feedSearchResult.addEventListener(SearchResultEvent.SEARCH_ADD_SUBSCRIBE, onAddSubscribe_handler);
			feedSearchResult.addEventListener(SearchResultEvent.SEARCH_OPEN_EXPLORER, onOpneExplorer_handler);
		}
		
		public function clearReference():void
		{
			dataProxy = null;
			feedSearchResult.removeEventListener(SearchResultEvent.SEARCH_ADD_SUBSCRIBE, onAddSubscribe_handler);
			feedSearchResult.removeEventListener(SearchResultEvent.SEARCH_OPEN_EXPLORER, onOpneExplorer_handler);
			facade.removeMediator(NAME);
		}
		
		override public function listNotificationInterests():Vector.<String>
		{
			return Vector.<String>([ApplicationFacade.LOGOUT]);
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case ApplicationFacade.LOGOUT:
					clearReference();
					break;
			}
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//events
		
		private function onAddSubscribe_handler(e:SearchResultEvent):void
		{
			var bs:BlogSourceVO = new BlogSourceVO();
			bs.blogName = e.gFeedEntry.trimHtmlTitle
			bs.blogLink = e.gFeedEntry.link;
			bs.blogRSS = e.gFeedEntry.url;
			var st:SearchResultLoader = new SearchResultLoader(bs, dataProxy, feedSearchResult);
			st.check();
		}
		
		private function onOpneExplorer_handler(e:SearchResultEvent):void
		{
			sendNotification(ApplicationFacade.AIR_EXPLORER_OPEN, e.gFeedEntry.link);
		}
	}
}

import flash.net.URLLoader;
import reader.model.IDataProxy;
import reader.model.vo.BlogSourceVO;
import flash.events.Event;
import flash.events.IOErrorEvent;
import mx.core.UIComponent;
import mx.controls.Alert;
import reader.core.ReaderResource;
import flash.net.URLRequest;
	
class SearchResultLoader
{
	private var proxy:IDataProxy;
	private var blogsource:BlogSourceVO;
	private var parent:UIComponent;
	
	public function SearchResultLoader(blogsource:BlogSourceVO, proxy:IDataProxy, parent:UIComponent)
	{
		this.blogsource = blogsource;
		this.proxy = proxy;
		this.parent = parent;
		
	}
	
	public function check():void
	{
		var loader:URLLoader = new URLLoader();
		loader.addEventListener(Event.COMPLETE, onBlogSourceLoad_handler);
		loader.addEventListener(IOErrorEvent.IO_ERROR, onBlogSourceError_handler);
		loader.load(new URLRequest(blogsource.blogRSS));
	}
	
	private function onBlogSourceLoad_handler(e:Event):void
	{
		var loader:URLLoader = e.target as URLLoader;
		loader.removeEventListener(Event.COMPLETE, onBlogSourceLoad_handler);
		loader.removeEventListener(IOErrorEvent.IO_ERROR, onBlogSourceError_handler);
		proxy.addSubscribe(blogsource);
	}
	
	private function onBlogSourceError_handler(e:Event):void
	{
		var loader:URLLoader = e.target as URLLoader;
		loader.removeEventListener(Event.COMPLETE, onBlogSourceLoad_handler);
		loader.removeEventListener(IOErrorEvent.IO_ERROR, onBlogSourceError_handler);
		Alert.show(ReaderResource.instance.ResizeHandler.addSubscribePanelIOError, "", 4, parent);
	}
}